using System;
using System.Collections;
using System.Collections.Generic;
using LitJson;

namespace Function.HotUpdate
{
    public class HotCheckNeedUpdate : HotStep
    {
        public override HotState GetHotState()
        {
            return HotState.CheckNeedUpdate;
        }

        public HotCheckNeedUpdate(HotContext hotContext, bool cb) : base(hotContext, cb)
        {
        }

        protected override IEnumerator OnExecute()
        {
            HotManager.Instance._commonTrace?.Invoke("hotfix_detect", new Dictionary<string, JsonData>()
            {
                { "network", 1 }
            });
            Version beforeVersion = _hotContext.BeforeVersion;
            Version remoteVersion = _hotContext._remoteVersion;

            // 检测是否强更
            var beforeBig = new Version(beforeVersion.Major, beforeVersion.Minor);
            var remoteBig = new Version(remoteVersion.Major, remoteVersion.Minor);
            if (beforeBig.CompareTo(remoteBig) < 0)
            {
                // TODO 弹强更面板
                HotManager.Instance._errorPop?.Invoke((int)HotUpdateErrorType.NeedStoreDown);
                $"[HotUpdate] 强更 {beforeBig} -> {remoteBig} storeUrl:{_hotContext._lighthouse.url_store}".Log();
                yield break;
            }

            _hotContext._needUpdate = false;
            if (beforeVersion.CompareTo(remoteVersion) < 0)
            {
                _hotContext._needUpdate = true;
            }

            SetSuccess(true);
            yield return Finish();
        }
    }
}